package com.suanfa.string;

/**
 * 字符串中的#号代表回退一个字符， 判断两个字符串是否相同
 */
public class BackDeleteString {

    public static void main(String[] args) {
        String a = "ab##";
        String b = "d#c#";
        System.out.println(ieEqauls(a, b));
    }


    public static boolean ieEqauls(String a, String b) {

        int i = a.length() - 1, j = b.length() - 1;
        int skipA = 0, skipB = 0;
        boolean result = true;
        while (i >= 0 || j >= 0) {
            while (i >= 0) {
                if (a.charAt(i) == '#') {
                    skipA++;
                } else if (skipA == 0) {
                    break;
                } else {
                    skipA--;
                }
                i--;
            }

            while (j >= 0) {
                if (b.charAt(j) == '#') {
                    skipB++;
                } else if (skipB == 0) {
                    break;
                } else {
                    skipB--;
                }
                j--;
            }

            if (i >= 0 && j >= 0) {
                if (a.charAt(i) != b.charAt(j)) {
                    result = false;
                    break;
                }
            } else if (i >= 0 || j >= 0) {//其中一组已经遍历完成，
                result = false;
                break;
            }
            i--;
            j--;
        }
        return result;
    }
}
